<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class OrderGoods extends BaseModel
{
    //定义表名
    protected $name = 'shequshop_car_order_goods';


    protected $append = [

        'refund_num'
    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:获取退款的数量
     * @author chenniang
     * @DataTime: 2021-04-12 10:46
     */
    public function getRefundNumAttr($value,$data){

        if(!empty($data['id'])){

            $refund_model = new RefundOrder();

            $num = $refund_model->refundNum($data['id']);

            return $num;
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataSelect($dis){

        $data = $this->where($dis)->order('id desc')->select()->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-22 11:12
     * @功能说明:添加商品子订单
     */
    public function orderGoodsAdd($order_goods,$order_id,$user_id){

        $goods_model = new Goods();

        $car_model   = new Car();

        foreach ($order_goods as $v){

//            $ser_status = $goods_model->where(['id'=>$v['goods_id']])->value('status');

            $goods = $goods_model->dataInfo(['id'=>$v['goods_id']]);

            if($goods['status']!=1){

                return ['code'=>500,'msg'=>'商品已经下架'];
            }

            $res = $this->checkIdentity($goods,$user_id);

            if(!empty($res['code'])){

                return $res;
            }

            $insert = [

                'uniacid'        => $v['uniacid'],

                'order_id'       => $order_id,

                'user_id'        => $user_id,

                'pay_type'       => 1,

                'goods_name'     => $v['title'],

                'goods_cover'    => $v['cover'],

                'goods_price'    => $v['price'],

                'true_price'     => round($v['true_price']/$v['num'],5),

                'pay_price'      => round($v['true_price'],2),

                'num'            => $v['num'],

                'can_refund_num' => $v['num'],

                'goods_id'       => $v['goods_id'],

                'circle'         => $goods['number'],

                'car_type_id'    => $goods['car_type_id'],


            ];

            $res = $this->dataAdd($insert);

            if($res!=1){

                return ['code'=>500,'msg'=>'下单失败'];
            }

            $order_goods_id = $this->getLastInsID();

            $this->updateSome($v,$order_goods_id);
        }
        //删除购物车
        $res = $car_model->where(['user_id'=>$user_id,'status'=>1])->delete();

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-23 17:33
     * @功能说明:检查购买者身份
     */
    public function checkIdentity($goods,$user_id){

        $driver_model = new CarDriver();
        //查看是否是专业赛车手
        $info = $driver_model->dataInfo(['user_id'=>$user_id,'status'=>2]);

        $car_type_model = new CarType();

        $car_type = $car_type_model->dataInfo(['id'=>$goods['car_type_id']]);

        if(!empty($info)&&empty($car_type['major'])){

            return ['code'=>500,'msg'=>$goods['title'].'只有普通车手可购买'];
        }

        if(empty($info)&&empty($car_type['norm'])){

            return ['code'=>50001,'msg'=>$goods['title'].'只有专业车手可购买'];
        }

        return true;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-09-22 10:46
     * @功能说明:添加车型
     */
    public function updateSome($order_goods,$order_goods_id){

        $car_connect_type = new CarTypeConnect();


        $car_type = $car_connect_type->where(['order_goods_id'=>0,'goods_id'=>$order_goods['goods_id']])->column('type_id');

        if(!empty($car_type)){

            foreach ($car_type as $k=> $value){

                $insert[$k]['goods_id'] = $order_goods['goods_id'];

                $insert[$k]['uniacid']  = $order_goods['uniacid'];

                $insert[$k]['order_goods_id']  = $order_goods_id;

                $insert[$k]['type_id']  = $value;

            }

            $car_connect_type->saveAll($insert);

        }

        return true;

    }







}